package com.mazewei.dev.service;

import com.mazewei.dev.dao.TeacherDao;
import com.mazewei.dev.dao.ClassDao;
import com.mazewei.dev.dao.CourseDao;
import com.mazewei.dev.entity.Teacher;
import com.mazewei.dev.entity.Class;
import com.mazewei.dev.entity.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 标注服务类
 * 用于根据名称查询编码并进行标注
 */
@Service
public class AnnotationService {
    
    @Autowired
    private TeacherDao teacherDao;
    
    @Autowired
    private ClassDao classDao;
    
    @Autowired
    private CourseDao courseDao;
    
    /**
     * 标注教师名称
     * 根据教师姓名查询教师编号，并拼接为"编号-姓名"的格式
     * 支持Excel中完整姓名与数据库中分离的姓名匹配
     * @param teacherName 教师姓名
     * @return 标注后的教师名称
     */
    public String annotateTeacher(String teacherName) {
        if (teacherName == null || teacherName.trim().isEmpty()) {
            return teacherName;
        }
        
        String trimmedName = teacherName.trim();
        Teacher teacher = null;
        
        // 方法1: 按姓+名组合查询（适用于数据库中姓名分离存储的情况）
        teacher = teacherDao.findByFullName(trimmedName);
        if (teacher != null && teacher.getTrNum() != null) {
            return teacher.getTrNum() + "-" + teacherName;
        }
        
        // 注释：已移除拆分姓名查询方法，因为相关的findByFirstNameAndName方法已被删除
        
        // 如果所有方法都找不到对应的编号，返回原名称
        return teacherName;
    }
    
    /**
     * 标注班级名称
     * 根据班级名称查询班级编号，并拼接为"编号-名称"的格式
     * @param className 班级名称
     * @return 标注后的班级名称
     */
    public String annotateClass(String className) {
        if (className == null || className.trim().isEmpty()) {
            return className;
        }
        
        Class clazz = classDao.findByName(className.trim());
        if (clazz != null && clazz.getClassNum() != null) {
            return clazz.getClassNum() + "-" + className;
        }
        
        // 如果找不到对应的编号，返回原名称
        return className;
    }
    
    /**
     * 标注课程名称
     * @param courseName 课程名称
     * @return 标注后的课程名称
     */
    public String annotateCourse(String courseName) {
        if (courseName == null || courseName.trim().isEmpty()) {
            return courseName;
        }
        
        Course course = courseDao.findByName(courseName.trim());
        if (course != null && course.getCourNum() != null) {
            return course.getCourNum() + "-" + courseName;
        }
        
        return courseName;
    }
    
    /**
     * 批量标注
     * @param teacherName 教师名称
     * @param className 班级名称
     * @param courseName 课程名称
     * @return 批量标注结果
     */
    public String batchAnnotate(String teacherName, String className, String courseName) {
        StringBuilder result = new StringBuilder();
        result.append("批量标注结果:\n");
        result.append("教师: ").append(annotateTeacher(teacherName)).append("\n");
        result.append("班级: ").append(annotateClass(className)).append("\n");
        result.append("课程: ").append(annotateCourse(courseName)).append("\n");
        return result.toString();
    }
}